package com.gameguru.mathgame;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import com.gameguru.mathgame.R.id;


public class ShowResult extends Activity {
	private DBHelper dbHelper;
	private GameHelper gameHelper;
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		this.setContentView(R.layout.result);	
		this.setResult();
	}
	private void setResult () {
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		TextView totalTV = (TextView) findViewById(id.textView2);
		TextView markTV = (TextView) findViewById(id.textView4);
		TextView timeTV = (TextView) findViewById(id.textView6);
		Integer total = bundle.getInt("totalQuestions");
		Integer mark = bundle.getInt("score");
		Long time = bundle.getLong("time");
		
		ArrayList<Integer> timelist = calculateTime(time);
		String timestr = new String();		
		if (timelist.get(0)!= null && timelist.get(0)!=0)timestr = timelist.get(0) +getString(R.string.days);
		if (timelist.get(1)!= null && timelist.get(1)!=0)timestr = timestr + timelist.get(1) +getString(R.string.hrs);
		if (timelist.get(2)!= null && timelist.get(2)!=0)timestr = timestr + timelist.get(2) +getString(R.string.mins);
		if (timelist.get(3)!= null && timelist.get(3)!=0)timestr = timestr + timelist.get(3) +getString(R.string.sec);
		if (timelist.get(4)!= null && timelist.get(4)!=0)timestr = timestr + timelist.get(4) +getString(R.string.msec);
	
		totalTV.setText(total.toString());
		markTV.setText(mark.toString());
		timeTV.setText(timestr);
		dbHelper = new DBHelper(getApplicationContext());
		gameHelper = new GameHelper(getApplicationContext());
		ContentValues contentValues = new ContentValues();
		contentValues.put(DBHelper.TOT_Q, total);
		contentValues.put(DBHelper.SCORE, mark);
		contentValues.put(DBHelper.TIME_TAKEN, time);
		contentValues.put(DBHelper.P_NAME, gameHelper.getPlayerName());
		contentValues.put(DBHelper.PLAY_DATE, System.currentTimeMillis());
		contentValues.put(DBHelper.TEST_TYPE, bundle.getInt(DBHelper.TEST_TYPE));
		dbHelper.save(contentValues);
	}
	public ArrayList<Integer> calculateTime(Long milliseconds) {
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		Long seconds = milliseconds/1000;
		int remainingMS = (int) (milliseconds%1000);
		int minutes = (int) (seconds/60);
		int remainingSec = (int) (seconds%60);
		int hours = (int) minutes/60;
		int remainingMin = hours%60;
		int days = hours/24;
		arrayList.add(days);
		arrayList.add(hours);
		arrayList.add(remainingMin);
		arrayList.add(remainingSec);
		arrayList.add(remainingMS);
		return arrayList;
		
	}
}
